package com.kitty.game.zhounianka.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.admin.message.ReqAdminMakeEquip;
import com.kitty.game.base.service.BagService;
import com.kitty.game.config.NPC;
import com.kitty.game.drop.newservice.NewDropService;
import com.kitty.game.equip.message.RespNotifyMiscEx;
import com.kitty.game.equip.service.EquipService;
import com.kitty.game.role.model.Account;
import com.kitty.game.role.model.Role;
import com.kitty.game.zhounianka.message.CmdMiJingShiLianBuyItem;
import com.kitty.game.zhounianka.message.MiJingShiLianEntity;
import com.kitty.game.zhounianka.message.MiJingShiLianInfo;
import com.kitty.game.zhounianka.message.MsgMiJingShiLian;
import com.kitty.mina.annotation.RequestMapping;
import com.kitty.mina.cache.DataCache;
import com.kitty.mina.cache.SessionUtils;
import com.kitty.mina.message.MessagePusher;
import lombok.extern.slf4j.Slf4j;
import org.apache.mina.core.session.IoSession;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Controller
public class MijingShiLianController {

    @Autowired
    Dao dao;
    @Autowired
    EquipService equipService;
    @Autowired
    BagService bagService;
    @RequestMapping
    public void MiJingShiLianBuyItem(IoSession session, CmdMiJingShiLianBuyItem cmdMiJingShiLianBuyItem) {
        int num = cmdMiJingShiLianBuyItem.getNum();
        /**需要先验证账号权限*/
        Role role = SessionUtils.getRoleBySession(session);
        List<MiJingShiLianEntity> roleList = dao.query(MiJingShiLianEntity.class, Cnd.where("id","=",Integer.valueOf(cmdMiJingShiLianBuyItem.getIndex())));
        if(roleList == null || roleList.size() == 0){
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("物品不存在"));
            return ;
        }
        if(num>99){
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("一次最多兑换99个"));
            return ;
        }
        MiJingShiLianEntity miJingShiLianInfo = roleList.get(0);

        int cost = miJingShiLianInfo.getCost() * num;
        int stoneNum = equipService.getCount("周年卡", false, role);
        if(cost>stoneNum){
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("周年卡数量不足"));
            return ;
        }

        //判断位置是否足够。
        short newPos =getVacancy(role);
        if (newPos <= 0) {
            MessagePusher.pushMessage(role, new RespNotifyMiscEx("很抱歉，你的包裹空位不足，请整理后再来吧。"));
            return ;
        }

        //扣减周年卡
        equipService.delRoloEquip("周年卡",cost, true, role);

        zhounianka(role);

        //发送奖励
        for (int i = 0; i < num; i++) {
            for (String s : miJingShiLianInfo.getName().split("\\+")) {
                SpringUtils.getBean(NewDropService.class).addRewardTask(role,s,"");
            }
        }


    }

    /**背包是否有足够的空位
     */
    short getVacancy(Role role){
        return bagService.getPos(role, false);
    }


    /**
     * 周年卡
     * @param role
     */
    public void zhounianka(Role role) {

        List<MiJingShiLianEntity> query = dao.query(MiJingShiLianEntity.class, Cnd.NEW());
        if (CollectionUtil.isNotEmpty(query)) {
            MsgMiJingShiLian msgMiJingShiLian = new MsgMiJingShiLian();
            List<MiJingShiLianInfo> miJingShiLianInfoList = new ArrayList<>();
            int stoneNum = equipService.getCount("周年卡", false, role);
            msgMiJingShiLian.setScore(stoneNum);
            int bindId = 1;
            for (MiJingShiLianEntity miJingShiLianInfo : query) {

                String[] names = miJingShiLianInfo.getName().split("-");
                if (names.length >= 2) {
                    String name = names[1].split("\\$")[0];
                    MiJingShiLianInfo miJingShiLianInfo1 = new MiJingShiLianInfo();
                    miJingShiLianInfo1.setIndex((byte)miJingShiLianInfo.getId());
                    miJingShiLianInfo1.setName(name);
                    miJingShiLianInfo1.setCost(miJingShiLianInfo.getCost());
                    miJingShiLianInfo1.setNum((short)miJingShiLianInfo.getNum());
                    miJingShiLianInfo1.setBind(bindId++);
                    miJingShiLianInfoList.add(miJingShiLianInfo1);
                }
            }
            msgMiJingShiLian.setMiJingShiLianInfoList(miJingShiLianInfoList);
            MessagePusher.pushMessages(role, msgMiJingShiLian);

        }
    }

}
